<template>
    <layout>
        <el-page-header @back="$router.back()" :content = this.content></el-page-header>
        <el-form label-width="80px">
            <el-form-item label="名称">
                <el-input v-model="form.name"></el-input>
            </el-form-item> 
            <el-form-item>
                <el-button type="primary" @click="save">保存</el-button>
            </el-form-item>
        </el-form>
    </layout>
</template>

<script>
export default {
    data() {
        return {
            form: {

            }
        }
    },
    created() {
        if(this.$route.params.id){
            this.$http.get(`role/get?id=${this.$route.params.id}`).then(res => {
                if(res.errcode == 0){
                    let data = res.data
                    this.form = data 
                    console.log(this.form )
                }
            })
            this.content = '编辑角色页面'

        } else{
            this.content = "新增角色页面"
        }
    },
    methods: {
        save() {
            if(this.$route.params.id) {
                console.log(this.form)
                this.$http.post(`/role/update` ,{
                    name: this.form.name,
                    id: this.form.id
                }).then(res => {
                    console.log(res.data)
                    if(res.errcode == 0){
                        this.$router.back()
                    }
                })
            }else{
                this.$http.post('/role/insert',this.form).then(res => {
                    if(res.errcode == 0){
                        this.$router.back()
                    }
                })
            }

        }
    }
}
</script>
<style>
.el-page-header{
    margin-bottom: 25px;
    margin-top: 5px;
}
</style>>
